package com.cms.controller.admin.div;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.controller.admin.BaseController;
import com.cms.domain.Feedback;
import com.cms.entity.Div;
import com.cms.entity.DivField;
import com.cms.service.DivFieldService;
import com.cms.service.DivService;
import com.cms.utils.FieldUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Controller - 自定义表字段
 * 
 * 
 * 
 */
@Controller("adminDivDivFieldController")
@RequestMapping("/admin/div/div_field")
public class DivFieldController extends BaseController {

	@Autowired
	private DivService divService;

	@Autowired
	private DivFieldService divFieldService;
	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(Integer divId, ModelMap modelMap) {
		setListQuery();
		List<DivField> divFields = divFieldService.findList(divId);
		modelMap.addAttribute("divFields", divFields);
		modelMap.addAttribute("divId", divId);
		return getView("div/div_field/index");
	}
    
    /**
     * 检查名称是否存在
     */
    @ResponseBody
	@RequestMapping("/checkName")
    public Boolean checkName(Integer divId,String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        return !divFieldService.nameExists(divId,name);
    }


	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(Integer divId, ModelMap modelMap) {
		modelMap.addAttribute("div", divService.getById(divId));
		return getView("div/div_field/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(DivField divField) {
		divField.setCreateDate(LocalDateTime.now());
		divField.setUpdateDate(LocalDateTime.now());
		divFieldService.save(divField);
		//数据库表
		Div div = divField.getDiv();
	    FieldUtils.add(div.getTableName(), divField.getName());
		return "redirect:"+getListQuery("/admin/div/div_field");
	}

	
	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id, ModelMap modelMap) {
		modelMap.addAttribute("divField", divFieldService.getById(id));
		return getView("div/div_field/edit");
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public String update(DivField divField) {
		divField.setUpdateDate(LocalDateTime.now());
		divFieldService.updateById(divField);
		return "redirect:"+getListQuery("/admin/div/div_field");
	}
	
   /**
     * 修改排序
     */
   	@ResponseBody
   	@RequestMapping("/updateSort")
    public Feedback updateSort(String sortArray){
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
			Integer id = jsonObject.getInteger("id");
            Integer sort = jsonObject.getInteger("sort");
            DivField divField = divFieldService.getById(id);
			divField.setSort(sort);
			divFieldService.updateById(divField);
        }
        return Feedback.success(new HashMap<>());
    }

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			for(Integer id:ids){
				DivField divField = divFieldService.getById(id);
				Div div = divField.getDiv();
				//数据库表
	            FieldUtils.delete(div.getTableName(), divField.getName());
				divFieldService.removeById(id);
			}
		}
		return Feedback.success(new HashMap<>());
	}

}